/*--------------------------------------传入时间格式，将date类型转成指定日期格式----------------------- -----------*/

Date.prototype.Format = function (fmt) { //author: meizz   
    var o = {  
        "M+": this.getMonth() + 1, //月份   
        "d+": this.getDate(), //日   
        "h+": this.getHours(), //小时   
        "m+": this.getMinutes(), //分   
        "s+": this.getSeconds(), //秒   
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度   
        "S": this.getMilliseconds() //毫秒   
    };  
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));  
    for (var k in o)  
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));  
    return fmt;  
}  
// 将指定的毫秒数加到此实例的值上
Date.prototype.addMilliseconds = function (value) { 
	var millisecond = this.getMilliseconds(); 
	this.setMilliseconds(millisecond + value); 
	return this; 
}; 
// 将指定的秒数加到此实例的值上
Date.prototype.addSeconds = function (value) { 
	var second = this.getSeconds(); 
	this.setSeconds(second + value); 
	return this; 
}; 
// 将指定的分钟数加到此实例的值上
Date.prototype.addMinutes = function (value) { 
	var minute = this.addMinutes(); 
	this.setMinutes(minute + value); 
return this; 
}; 
// 将指定的小时数加到此实例的值上
Date.prototype.addHours = function (value) { 
	var hour = this.getHours(); 
	this.setHours(hour + value); 
return this; 
}; 
// 将指定的天数加到此实例的值上
Date.prototype.addDays = function (value) { 
	var date = this.getDate(); 
	this.setDate(date + value); 
return this; 
}; 
// 将指定的星期数加到此实例的值上
Date.prototype.addWeeks = function (value) { 
	return this.addDays(value * 7); 
}; 
// 将指定的月份数加到此实例的值上
Date.prototype.addMonths = function (value) { 
	var month = this.getMonth(); 
	this.setMonth(month + value); 
return this; 
}; 
// 将指定的年份数加到此实例的值上
Date.prototype.addYears = function (value) { 
	var year = this.getFullYear(); 
	this.setFullYear(year + value); 
return this; 
}; 

//定义默认参数
var _options = {
  ZH: {
    dayNames: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
    shortDayNames: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
    monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
    shortMonthNames: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
  },
  US: {
    dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
    shortDayNames: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
    monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
    shortMonthNames: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
  }
};
	/**
	* 是否为空
	* @param str
	* @returns {boolean}
	*/
	function isNotEmpty(str) {
	if (str != '' && str != null && typeof str != 'undefined') {
	  return true;
	}
	return false;
	};
	
	//定义内部常量
	var WEEKTYPE =  {
	  ZH_DAYNAME: 0,
	  ZH_SDAYNAME: 1,
	  US_DAYNAME: 2,
	  US_SDAYNAME: 3,
	};
	/**
	 * 格式化时间
	 * @param date
	 * @param fmt
	 * @returns {*}
	 * @author wesley
	 */
var	format =  function (date, fmt) {
	  var o = {
	    'M+': date.getMonth() + 1, //月份
	    'd+': date.getDate(), //日
	    'h+': date.getHours(), //小时
	    'm+': date.getMinutes(), //分
	    's+': date.getSeconds(), //秒
	    'q+': Math.floor((date.getMonth() + 3) / 3), //季度
	    'S': date.getMilliseconds() //毫秒
	  };
	  if(!isNotEmpty(fmt)){
	    fmt = 'yyyy-MM-dd hh:mm:ss';
	  }
	  if (/(y+)/.test(fmt)) {
	    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
	  }
	  for (var k in o) {
	    if (new RegExp('(' + k + ')').test(fmt)) {
	      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
	    }
	  }
	  return fmt;
	}
	
var	formatToDate =  function (dateStr) {
	  if(isNotEmpty(dateStr) ){
	    return new Date(Date.parse(dateStr.replace(/-/g,   "/")));
	  }
	  return '';
	}
	
	/**
	 * 得到一天的开始 date类型
	 * @param date
	 * @author wesley
	 */
var getDateStart =  function (date) {
	  var fmt = 'yyyy-MM-dd';
	  var dateStartStr = getDateStartStr(date, fmt);
	  var startTime = new Date(Date.parse(dateStartStr));
	  return startTime;
	}
	
	/**
	 * 得到一天的开始 str 类型
	 * @param date
	 * @author wesley
	 */
var	getDateStartStr =  function (date, fmt) {
	  if (typeof fmt == 'undefined') {
	    fmt = 'yyyy-MM-dd';
	  }
	  var dateStr = format(date, fmt);
	  dateStr += ' 00:00:00';
	  return dateStr;
	}
	
	/**
	 * 得到一天的结束 date类型
	 * @param date
	 * @author wesley
	 */
var	getDateEnd = function (date) {
	  var fmt = 'yyyy-MM-dd';
	  var dateEndStr = getDateEndStr(date, fmt);
	  var endTime = new Date(Date.parse(dateEndStr));
	  return endTime;
	}
	
	/**
	 * 得到一天的结束 str 类型
	 * @param date
	 * @author wesley
	 */
var	getDateEndStr =  function (date, fmt) {
	  if (typeof fmt == 'undefined') {
	    fmt = 'yyyy-MM-dd';
	  }
	  var endStr = format(date, fmt);
	  endStr += ' 23:59:59';
	  return endStr;
	}
	
	/**
	 * 两个时间比较大小
	 * @param d1
	 * @param d2
	 * @author wesley
	 */
var	compareDate = function (d1, d2) {
	  if (d1 && d2) {
	    if (d1.getTime() > d2.getTime()) {
	      return 1;
	    } else if (d1.getTime() == d2.getTime()) {
	      return 0;
	    } else if (d1.getTime() < d2.getTime()) {
	      return -1;
	    }
	  }
	}
	
	/**
	 * 得到星期几, 支持中英
	 * @param date
	 * @param type
	 * @returns {string}
	 * @author wesley
	 */
var	getWeek =  function (date, type) {
	  if (date) {
	    if (!isNotEmpty(type)) {
	      type = 0;
	    }
	    var index = date.getDay();
	    var dateStr = '';
	    switch (type) {
	      case WEEKTYPE.ZH_DAYNAME:
	        dateStr = _options.ZH.dayNames[index];
	        break;
	      case WEEKTYPE.ZH_SDAYNAME:
	        dateStr = _options.ZH.shortDayNames[index];
	        break;
	      case WEEKTYPE.US_DAYNAME:
	        dateStr = _options.US.dayNames[index];
	        break;
	      case WEEKTYPE.US_SDAYNAME:
	        dateStr = _options.US.shortDayNames[index];
	        break;
	    }
	    return dateStr;
	  }
	}
	
	/**
	 * 是否为闰年
	 * @param date
	 * @returns {boolean}
	 * @author wesley
	 */
var	isLeapYear = function (date) {
	  if (date instanceof Date) {
	    return (0 == date.getYear() % 4 && (( date.getYear() % 100 != 0) || (date.getYear() % 400 == 0)));
	  }
	  console.warn('argument format is wrong');
	  return false;
	}
	
	/**
	 * 字符串是否为正确的时间格式，支持 - /
	 * @param dateStr
	 * @returns {boolean}
	 * @author wesley
	 */
var	isValidDate = function (dateStr) {
	  if (isNotEmpty(dateStr)) {
	    var r= dateStr.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
	    if(r==null){
	      return false;
	    }
	    var d=new Date(r[1],r[3]-1,r[4]);
	    var num = (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);
	    return (num!=0);
	  }
	}
	
	/**
	 * 增加天数
	 * @param date
	 * @param dayNum
	 * @author wesley
	 */
var	addDay = function (date, dayNum) {
	  if(isNotEmpty(date) && isNotEmpty(dayNum) && date instanceof Date && typeof dayNum == 'number'){
	    date.setDate(date.getDate() + dayNum);
	  }else{
	    console.warn('date or dayNum format wrong');
	  }
	  return date;
	}
	
var	addDayStr = function (dateStr, dayNum) {
	  var date ='';
	  if(isNotEmpty(dateStr) && isNotEmpty(dayNum) && typeof dayNum == 'number'){
	    date = formatToDate(dateStr);
	    date.setDate(date.getDate() + dayNum);
	  }else{
	    console.warn('dateStr or dayNum format wrong');
	  }
	  return date;
	}
	
	/**
	 * 增加月份
	 * @param date
	 * @param dayNum
	 * @author wesley
	 */
var	addMonth =  function (date, monthNum) {
	  if(isNotEmpty(date) && isNotEmpty(monthNum) && date instanceof Date && typeof monthNum == 'number'){
	    date.setMonth(date.getMonth() + monthNum);
	  }else{
	    console.warn('date or monthNum format wrong');
	  }
	  return date;
	}
	
var	addMonthStr = function (dateStr, monthNum) {
	  var date ='';
	  if(isNotEmpty(dateStr) && isNotEmpty(monthNum) && typeof monthNum == 'number'){
	    date = formatToDate(dateStr);
	    date.setMonth(date.getMonth() + monthNum);
	  }else{
	    console.warn('date or monthNum format wrong');
	  }
	  return date;
	}
	
	/**
	 * 增加年份
	 * @param date
	 * @param dayNum
	 * @author wesley
	 */
var	addYear =  function (date, yearNum) {
	  if(isNotEmpty(date) && isNotEmpty(yearNum) && date instanceof Date && typeof yearNum == 'number'){
	    date.setYear(date.getFullYear() + yearNum);
	  }else{
	    console.warn('date or yearNum format wrong');
	  }
	  return date;
	}
	
var	addYearStr = function (dateStr, yearNum) {
	  var date = '';
	  if(isNotEmpty(dateStr) && isNotEmpty(yearNum) && typeof yearNum == 'number'){
	    date = formatToDate(dateStr);
	    date.setYear(date.getFullYear() + yearNum);
	  }else{
	    console.warn('date or yearNum format wrong');
	  }
	  return date;
	}	